import requests
import json
from bs4 import BeautifulSoup
import re
import jmcomic
import time


url = 'https://tieba.baidu.com/f?kw=%E7%A6%BE%E9%87%8E&ie=utf-8&pn=300'
option = jmcomic.create_option_by_file("option.yml")
headers = {
    'cookie':'',
    'referer':'https://tieba.baidu.com/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'
}
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
a_tags = soup.find_all('a')

search_string = '/p/'
for tag in a_tags:
    
    href = tag.get('href')
    if href and search_string in href and 'baidu' not in href:
        print(href)
        srcurl = 'https://tieba.baidu.com'+href
        time.sleep(20)
        pagehtml = requests.get(srcurl, headers=headers,stream=True)
        asoup = BeautifulSoup(pagehtml.text, 'html.parser')
        div_tags = asoup.find_all('div', class_='d_post_content j_d_post_content')
        for div in div_tags:
            print(div.get_text(strip=True))
            text = div.get_text(strip=True)
            numbers = re.findall(r'\d+', text)
            number_string = ''.join(numbers)
            if len(number_string) >= 4:  
                print(number_string)
                try:
                    jmcomic.download_album(number_string, option)
                except Exception as e:
                    print(f"Error downloading album: {e}")
            else:
                print(".")
        else:
            print(f"nohero in {href}")

